package com.jessechanrui.template.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射案例
 *
 * @author chenrui
 * @date 2024/9/25
 */
public class ReflectCase {
    public static void main(String[] args) {
        try {
            Class<?> aClass = Class.forName("com.jessechanrui.template.reflection.User");
            Constructor<?>[] constructors = aClass.getConstructors();
            for (Constructor<?> constructor : constructors) {
                System.out.println(constructor.getName());
            }
            Method getNameMethod = aClass.getMethod("getName", null);
            Object o = aClass.newInstance();
            getNameMethod.invoke(o, null);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

    }
}
